
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

static rt_adc_device_t dev_;

rt_uint16_t bat_v;
rt_int16_t v;

static void thread_(void *param)
{
    while (1)
    {
        v = rt_adc_voltage(dev_, 8);
        bat_v = v / 10 * 110;
        rt_kprintf("rt_adc_voltage: %d\n", v);
        rt_kprintf("bat_v: %d\n", bat_v);
        rt_thread_mdelay(1000);
    }
}

void app_battery_init(void)
{
    rt_err_t ret;
    dev_ = (rt_adc_device_t)rt_device_find("adc2");
    if (!dev_)
        goto exit;

    ret = rt_adc_enable(dev_, 8);
    rt_kprintf("rt_adc_enable ret: %d\n", ret);

    rt_thread_t t = rt_thread_create("bat_thread", thread_, RT_NULL, 2048, 25, 20);
    if (!t)
        goto exit;

    rt_thread_startup(t);

    return;
exit:
    rt_kprintf("error\n");
    return;
}
